package com.wyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.common.response.PageInfo;
import com.wyh.entity.AdminGiveRank;
import com.wyh.mapper.AdminGiveRankMapper;
import com.wyh.request.AdminGiveRankRequest;
import com.wyh.service.IAdminGiveRankService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 打赏排行业务层处理
 *
 * @author WangYuHang
 * @date 2024-03-03
 */
@Slf4j
@Service
@AllArgsConstructor
public class AdminGiveRankServiceImpl implements IAdminGiveRankService {

    private final AdminGiveRankMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdminGiveRank add(AdminGiveRankRequest request) {
        AdminGiveRank entity = AdminGiveRank.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AdminGiveRankRequest request) {
        AdminGiveRank entity = AdminGiveRank.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public AdminGiveRank getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminGiveRank> pageList(AdminGiveRankRequest request) {
        Page<AdminGiveRank> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<AdminGiveRank> query = this.buildWrapper(request);
        IPage<AdminGiveRank> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<AdminGiveRank> list(AdminGiveRankRequest request) {
        LambdaQueryWrapper<AdminGiveRank> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<AdminGiveRank> buildWrapper(AdminGiveRankRequest param) {
        LambdaQueryWrapper<AdminGiveRank> query = new LambdaQueryWrapper<>();
        if (param.getUid() != null){
               query.eq(AdminGiveRank::getUid ,param.getUid());
        }
        if (param.getGiveSum() != null){
               query.eq(AdminGiveRank::getGiveSum ,param.getGiveSum());
        }
        if (param.getIsDelete() != null){
               query.eq(AdminGiveRank::getIsDelete ,param.getIsDelete());
        }
        if (param.getCreateUserId() != null){
               query.eq(AdminGiveRank::getCreateUserId ,param.getCreateUserId());
        }
        if (param.getUpdateUserId() != null){
               query.eq(AdminGiveRank::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
